<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Question
 *
 * @author tuancasi
 */
class Question extends CActiveRecord {

    const TYPE_GREETING = 1;
    const TYPE_QUESTION = 2;
    const DELETED = 1;

    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return "{{questions}}";
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	);
    }

    public function rules() {
	parent::rules();
	return array(
	    array('text, type', 'required', 'on' => 'insert'),
	    array('question_id, list_answer, is_delete', 'safe'),
	    array('text, type', 'safe', 'on' => 'search')
	);
    }

    /**
     * get questions that admin created
     */
    public static function getQuestions() {

	$aQuestions = Yii::app()->cache->get('aQuestions');
	if ($aQuestions === FALSE) {
	    $aQuestions = array();
	    $aQuestions = Yii::app()->db->createCommand('SELECT * FROM {{questions}} WHERE type=' . self::TYPE_QUESTION)->queryAll();
	    foreach ($aQuestions as &$question) {
		$question['list_answer'] = unserialize($question['list_answer']);
	    }
	    Yii::app()->cache->set('aQuestions', $aQuestions);
	}
	return $aQuestions;
    }

    /**
     * get greeting
     */
    public static function getGreetings() {

	$aQuestions = Yii::app()->cache->get('aGreetings');
	if ($aQuestions === FALSE) {
	    $aQuestions = array();
	    $questions = Yii::app()->db->createCommand('SELECT * FROM {{questions}} WHERE type=' . self::TYPE_GREETING)->queryAll();

	    foreach ($questions as $question) {
		$aQuestions[$question['question_id']] = $question['text'];
	    }
	    Yii::app()->cache->set('aGreetings', $aQuestions);
	}
	return $aQuestions;
    }

    public static function getListType() {
	return array(
	    self::TYPE_GREETING => 'Greeting',
	    self::TYPE_QUESTION => 'Question'
	);
    }

    public function getType() {
	$aQuestion = Question::getListType();
	return $aQuestion[$this->type];
    }

    public function search() {
	$criteria = new CDbCriteria();
	$criteria->compare('text', $this->text, TRUE);
	$criteria->compare('type', $this->type);
	$criteria->addCondition('');
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    'question_id' => CSort::SORT_DESC
		)
	    )
	));
    }

    public function afterSave() {
	parent::afterSave();
	_clearCache('aGreetings');
	_clearCache('aQuestions');
    }

    public function afterDelete() {
	parent::afterDelete();
	_clearCache('aGreetings');
	_clearCache('aQuestions');
    }

}

